package agent.preferences;

import model.Actions;

import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.preference.*;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbench;

import services.Analizer;
import agent.Activator;

/**
 * This class represents a preference page that
 * is contributed to the Preferences dialog. By 
 * subclassing <samp>FieldEditorPreferencePage</samp>, we
 * can use the field support built into JFace that allows
 * us to create a page that is small and knows how to 
 * save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They
 * are stored in the preference store that belongs to
 * the main plug-in class. That way, preferences can
 * be accessed directly via the preference store.
 */

public class AgentPreferencePage
	extends FieldEditorPreferencePage
	implements IWorkbenchPreferencePage {

	public AgentPreferencePage() {
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("Configure the agent");
	}
	
	/**
	 * Creates the field editors. Field editors are abstractions of
	 * the common GUI blocks needed to manipulate various types
	 * of preferences. Each field editor knows how to save and
	 * restore itself.
	 */
	public void createFieldEditors() {

		addField(new ComboFieldEditor(PreferenceConstants.EMA_VALUE_PREFERENCE,"Choose ema value",
				new String[][] { { "1", "1" }, {"2", "2" }, {"3", "3" }, {"4", "4" }, {"5", "5" }, {"6", "6" } 
				, {"7", "7" }, {"8", "8" }, {"9", "9" }}, 
				getFieldEditorParent()));
		
		addField(new StringFieldEditor(PreferenceConstants.BUFFER_SIZE, "Buffer size:", getFieldEditorParent()));
	}
	
	/**
	 * Valido los campos de las preferencias cuando son modificados
	 */
	public void propertyChange(PropertyChangeEvent event) {
		 if(event.getProperty().equals("field_editor_value")){
			if (event.getSource() instanceof StringFieldEditor) {
				   if(((StringFieldEditor)event.getSource()).getPreferenceName().equals(PreferenceConstants.BUFFER_SIZE)) {
				     if (doIntegerPositiveValidation((String)event.getNewValue())){
						setValid(true);
						setErrorMessage(null);						
						super.performApply();
						super.propertyChange(event);
						Actions.ACTIONS_NUMBER=(new Integer((String)event.getNewValue()));
				     }
				     else {
						setValid(false);
						setErrorMessage("Value must be a positive integer greater than zero");
				     }
				   }
				 }
				else
			 	if (event.getSource() instanceof ComboFieldEditor) {
					   if(((ComboFieldEditor)event.getSource()).getPreferenceName().equals(PreferenceConstants.EMA_VALUE_PREFERENCE)) {
						  setValid(true);
						  setErrorMessage(null);						
						  super.performApply();
						  super.propertyChange(event);
						  Analizer.EMA_VALUE=(new Integer((String)event.getNewValue()));
					   }
				 }
		 }
	}
	
	/**
	 * Valido que el string pasado como parametro sea un entero positivo mayor que cero
	 * @param newValue
	 * @return
	 */
	public boolean doIntegerPositiveValidation (String newValue) {
		if ((!newValue.isEmpty())&&(newValue.trim().matches("^\\d*$"))) {
			Integer newValueInt = new Integer(newValue);
			if (newValueInt.intValue()>0) {
				return true;
			} 
		}
		return false;
	}
		
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench) {
	}
	
}